﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;

namespace RMS
{
    public partial class frmInsertCustomerInfo : ICommonForm
    {
        
        public frmInsertCustomerInfo()
        {
            InitializeComponent();
        }

        public frmInsertCustomerInfo(Customer customer)
        {
            InitializeComponent();

            DisplayCustomerInfo(customer);
        }

        private void DisplayCustomerInfo(Customer customer)
        {
            if (customer != null)
            {
                txtCusCode.Text = customer.ID;
                txtName.Text = customer.Name;
                txtPhone.Text = customer.PhoneNo;
                txtEmail.Text = customer.Email;
                txtAddress.Text = customer.Address;
                txtVipCode.Text = customer.VipCode;

                if (customer.IsVip.HasValue && customer.IsVip.Value.Equals(true))
                {
                    rbIsVip.Checked = true;
                    txtVipCode.Enabled = true;
                }
                else
                {
                    rbNotVip.Checked = false;
                    txtVipCode.Enabled = false;
                }                
            }
        }

        //public string result
        //{
        //    get
        //    {
        //        return String.Format("{0}#{1}#{2}#{3}#{4}#{5}#{6}", txtCusCode.Text.Trim(), txtName.Text.Trim(), txtAddress.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim(), Convert.ToString(rbIsVip.Checked), txtVipCode.Text.Trim());
        //    }
        //}

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCusCode.Text.Trim()))
            {
                MessageBox.Show("Hãy điền mã khách hàng.", "Chú ý");
                return;
            }
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("Hãy điền tên khách hàng.", "Chú ý");
                return;
            }
            if (rbIsVip.Checked && string.IsNullOrEmpty(txtVipCode.Text.Trim()))
            {
                MessageBox.Show("Hãy điền mã thẻ VIP.", "Chú ý");
                return;
            }
            
            CustomersMng manager = new CustomersMng();
            if (!string.IsNullOrEmpty(txtVipCode.Text.Trim()))
            {
                Customer item = manager.GetOne(txtCusCode.Text.Trim());
                if (item == null)
                {
                    Customer newCustomer = new Customer()
                    {
                        ID = txtCusCode.Text.Trim(),
                        Name = txtName.Text.Trim(),
                        Address = txtAddress.Text.Trim(),
                        PhoneNo = txtPhone.Text.Trim(),
                        Email = txtEmail.Text.Trim(),
                        IsVip = rbIsVip.Checked,
                        VipCode = txtVipCode.Text.Trim(),
                        NumberOfInvoice = 0,
                        TotalBuyingCost = 0
                    };


                    //add new customer
                    manager.AddItem(newCustomer);
                    frmSellInventory.customerInfo = newCustomer;
                }
                else
                {
                    frmSellInventory.customerInfo = item;
                }
            }
            

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData(txtCusCode.Text.Trim());
        }

        private void LoadData(string id)
        {
            CustomersMng _customersMng = new CustomersMng();
            var item = _customersMng.GetOne(id);
            if (item == null)
            {
                //MessageBox.Show("Không tìm thấy mã khách hàng này. Xin vui lòng kiểm tra lại", "Chú ý");
                rbNotVip.Checked = rbIsVip.Enabled = rbNotVip.Enabled = true;
                txtVipCode.Enabled = rbIsVip.Checked;
                txtAddress.Text =
                txtEmail.Text =
                txtName.Text =
                txtVipCode.Text =
                txtPhone.Text = "";
                return;
            }
            txtName.Text = item.Name;
            rbIsVip.Checked = Convert.ToBoolean(item.IsVip);
            rbNotVip.Checked = !Convert.ToBoolean(item.IsVip);
            if (rbIsVip.Checked)
            {
                txtVipCode.Text = Convert.ToString(item.VipCode);
                rbIsVip.Enabled = rbNotVip.Enabled = false;
                txtVipCode.Enabled = false;
            }
            //txtPhone.Text = item.PhoneNo;
            //txtEmail.Text = item.Email;
            //txtAddress.Text = item.Address;            
        }

        private void frmInsertCustomerInfo_Load(object sender, EventArgs e)
        {
            CustomersMng _customersMng = new CustomersMng();
            var autoCompleteSrc = _customersMng.GetAllItemIds();
            if (autoCompleteSrc.Count > 0)
            {
                var idCollection = new AutoCompleteStringCollection();
                idCollection.AddRange(autoCompleteSrc.ToArray());
                txtCusCode.AutoCompleteCustomSource = idCollection;
                txtCusCode.DataSource = autoCompleteSrc.ToArray();
            }            
        }

        private void txtCusCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCusCode.Text))
                return;
            btnSearch_Click(null, null);
        }

        private void rbIsVip_CheckedChanged(object sender, EventArgs e)
        {
            txtVipCode.Enabled = rbIsVip.Checked;
        }

        private void txtCusCode_Leave(object sender, EventArgs e)
        {
            
        }

        private void txtCusCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }
    }
}
